#include "stdio.h"
#include "stdlib.h"

typedef int DataType;
#define M 100
typedef struct {
    DataType *base;
    int front;
    int rear;
} SqQueue;
//(1) InitQueue (&Q) //构造空队列
int InitQueue(SqQueue *Q) {
    Q->base = (DataType *)malloc(sizeof(DataType) * M);
    Q->front = 0;
    Q->rear = 0;
    return 0;
}
//(2) DestroyQueue (&Q) //销毁队列
int DestroyQueue(SqQueue *Q) {
    free(Q->base);
    Q->base = NULL;
    Q->front = 0;
    Q->rear = 0;
    return 0;
}
//(3) ClearQueue (&S) //清空队列
int ClearQueue(SqQueue *Q) {
    Q->front = 0;
    Q->rear = 0;
    return 0;
}
//(4) QueueFull(S) //判断队列是否满
int QueueFull(SqQueue *Q) {
    if (Q->rear == Q->front) {
        return 1;
    }
    return 0;
}
//(4) QueueEmpty(S) //判空. 空—TRUE
int QueueEmpty(SqQueue *Q) {
    if (Q->front == Q->rear) {
        return 1;
    }
    return 0;
}
//(5) QueueLength(Q) //取队列长度
int QueueLength(SqQueue *Q) {
    return Q->rear - Q->front;
}
//(6) GetHead (Q,&e) //取队头元素,
int GetHead(SqQueue *Q, DataType *e) {
    if (QueueEmpty(Q)) {
        return -1;
    }
    *e = Q->base[Q->front];
    return 0;
}
//(7) EnQueue (&Q,e) //入队列
int EnQueue(SqQueue *Q, DataType e) {
    if (QueueFull(Q)) {
        return -1;
    }
    Q->base[Q->rear] = e;
    Q->rear = (Q->rear + 1) % M;
    return 0;
}
//(8) DeQueue (&Q,&e) //出队列
int DeQueue(SqQueue *Q, DataType *e) {
    if (QueueEmpty(Q)) {
        return -1;
    }
    *e = Q->base[Q->front];
    Q->front = (Q->front + 1) % M;
    return 0;
}
//(9) QueueTraverse(Q,visit()) //遍历
int QueueTraverse(SqQueue *Q, void (*visit)(DataType)) {
    int i;
    for (i = Q->front; i!= Q->rear; i = (i + 1) % M) {
        visit(Q->base[i]);
    }
    return 0;
}